﻿using com.guojin.mj.net.message.game;
using com.guojin.mj.net.message.login;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public delegate void RoomNameChangeEventHandler(string roomName);
public delegate void BotBetChangeEventHandler(int botBet);
public delegate void RoomIDChangeEventHandler(string roomID);

public class GameRoomModel
{
    private static GameRoomModel instance;
    public static GameRoomModel GetInstance()
    {
        if (instance == null)
        {
            instance = new GameRoomModel();
        }
        return instance;
    }

    public event RoomNameChangeEventHandler RoomNameChangeEvent;
    public event BotBetChangeEventHandler BotBetChangeEvent;
    public event RoomIDChangeEventHandler RoomIDChangeEvent;

    private string roomName;
    private int botScore;
    private string roomID;

    public string RoomName
    {
        get { return roomName; }
        set
        {
            roomName = value;
            if (RoomNameChangeEvent != null)
            {
                RoomNameChangeEvent(roomName);
            }
        }
    }
    public string RoomID
    {
        get { return roomID; }
        set
        {
            roomID = value;
            if (RoomIDChangeEvent != null)
            {
                RoomIDChangeEvent(RoomID);
            }
        }
    }

    public bool existRoom = true;
    public string CreateRoomUserName { get; set; }
    public int CreateRoomUserId { get; set; }
    public string CreateRoomTime { get; set; }
    public int CurrentUserNum { get; set; }
    public int MaxUser { get; set; }
    public int CurrentChapter { get; set; }                        // 当前局数
    public int RoomState { get; set; }
    public bool EmpowerTakeIn { get; set; }
    public bool AutoCompute { get; set; }
    public bool VieForZhuang { get; set; }
    public int MaxPlayerCount { get; set; }
    public int SameNiu { get; set; }
    public int OneTimesBet { get; set; }
    public int RoomType { get; set; }
    public int BotScore
    {
        get { return botScore; }
        set
        {
            botScore = value;
            if (BotBetChangeEvent != null)
            {
                BotBetChangeEvent(botScore);
            }
        }
    }
    public int MinTakeInScore { get; set; }
    public int MaxTakeInScore { get; set; }
    public int RoomExistenceTime { get; set; }
    public string  ClubOrLianMengName { get; set; }
    public string ClubOrLianMengID { get; set; }
    public string mingpaimos { get; set; }

    public int zhuangIndex = -1;
    public bool AutoNiu;
    public bool AutoBet;

    public  void SaveRoomInfoToGameRoomModel(GameRoomInfoRet GTI)
    {
        CreateRoomUserId =GTI.CreateUserId;
        GameData.GetInstance().roomHostUserID = GTI.CreateUserId.ToString();
        for (int i = 0; i < GTI.gameUser.Count; i++)
        {
            if (GTI.gameUser[i].userId == GTI.CreateUserId)
            {
                CreateRoomUserName = GTI.gameUser[i].userName;
            }
        }
        ClubOrLianMengName = GTI.ClubOrLianMengName;
        CreateRoomTime =GTI.createTime;
        RoomID = GTI.roomID;
      
        CurrentUserNum = GTI.currentUserNum;
        MaxUser =GTI.maxUser;
        CurrentChapter = GTI.currentChapterNum;
    }

    public Dictionary<int, Player> playerDic = new Dictionary<int, Player>();                          //坐下的玩家
    public Dictionary<string, Player> waitPlayerDic = new Dictionary<string, Player>();            //全部的玩家
    
    public void GameStartAppendPlayersToPlayerDic(List<GameUserInfoRet> gameUser)
    {
        playerDic.Clear();
        waitPlayerDic.Clear();

        for (int i = 0; i < gameUser.Count; i++)
        {
            if (gameUser[i].locationIndex >= 0)
            {
                TooL.LoadPrefab(GameRoomPage.GetInstance().loadPointGroup[GameController.GetInstance().intArr[gameUser[i].locationIndex]].
                  gameObject.transform.FindChild("headPoint").gameObject, "playerHead");

                TooL.LoadPrefab(GameRoomPage.GetInstance().loadPointGroup[GameController.GetInstance().intArr[gameUser[i].locationIndex]].
                  gameObject.transform.FindChild("cardPoint").gameObject, "playerCard");

                Player player = GameRoomPage.GetInstance().loadPointGroup[GameController.GetInstance().intArr[gameUser[i].locationIndex]].
                   gameObject.transform.FindChild("headPoint/playerHead").gameObject.AddComponent<Player>();

                playerDic.Add(gameUser[i].locationIndex, player);

                player.InitHeadAndNameSaveUserInfo(gameUser[i]);
            }
            else
            {
                if (GameRoomModel.GetInstance().waitPlayerDic.ContainsKey(gameUser[i].userId.ToString()))
                {
                    GameRoomModel.GetInstance().waitPlayerDic[gameUser[i].userId.ToString()].SaveUserInfoToThis(gameUser[i]);
                }
                else
                {
                    Player player = new Player();

                    GameRoomModel.GetInstance().waitPlayerDic.Add(gameUser[i].userId.ToString(), player);

                    player.SaveUserInfoToThis(gameUser[i]);
                }
            }
        }
    }

}
